package com.ald.crossex.action.client;

import com.ald.crossex.dto.client.LoginRequest;
import com.ald.crossex.dto.client.LoginResponse;
import com.ald.crossex.entity.TCeCoreUser;
import com.ald.crossex.service.UserService;
import com.ald.crossex.util.ErrorCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lishijiong on 15/4/29.
 */
@Controller
@RequestMapping(value = "/client/")
public class UserController {

    private static Logger log = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    /**
     * login method
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public LoginResponse login(@RequestBody LoginRequest request) {
        LoginResponse response = new LoginResponse();
        try {
            TCeCoreUser user = userService.getTCeCoreUserByUsernameAndPassword(request.getUsername(), request.getPassword());
            if (user == null) {
                response.setResultCode(ErrorCode.OPTION_ERROR.getCode());
                response.setResultMsg("登陆失败！");
            } else {
                response.setResultCode(ErrorCode.OPTION_SUCCESS.getCode());
                response.setResultMsg("登陆成功！");

            }
        } catch (Exception e) {
            log.error("client login error.username:" + request.getUsername(), e);
            response.setResultCode(ErrorCode.INTERNAL_EXCEPTION.getCode());
            response.setResultMsg(ErrorCode.INTERNAL_EXCEPTION.getMessage());

        }
        return response;
    }

}
